package com.mingmen.supplier.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.supplier.R;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.NoticeModel;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.ScalableCardHelper;
import com.mingmen.supplier.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class NoticeDetailActivity extends AppCompatActivity implements NoticeDetailAdapter.OnitemClick, ScalableCardHelper.OnPageChangeListener {

    String token;
    List<NoticeModel.DataBean> mylist = new ArrayList<>();
    NoticeDetailAdapter noticeDetailAdapter;
    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.linear_return)
    LinearLayout linearReturn;
    @BindView(R.id.layout_toolbar)
    RelativeLayout layoutToolbar;
    @BindView(R.id.rv_photo_list)
    RecyclerView rvPhotoList;

    String startTime;
    String endTime;
    RecyclerView recyclerView;
    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;
    String position;

    int scroll_Toposition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticedetail);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
//        linearLayoutManager = new LinearLayoutManager(this);
        token = (String) SharePreferenceUtil.get(this, "token", "");
        startTime = getIntent().getStringExtra("startTime");
        endTime = getIntent().getStringExtra("endTime");
        position = getIntent().getStringExtra("position");
        scroll_Toposition = Integer.valueOf(position);
        recyclerView = (RecyclerView) findViewById(R.id.rv_photo_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
//        recyclerView.setAdapter(new JingLiRuKuAdapter(getActivity(), dataBeanList));
        noticeDetailAdapter = new NoticeDetailAdapter(this, mylist);
        recyclerView.setAdapter(noticeDetailAdapter);
        ScalableCardHelper cardHelper = new ScalableCardHelper(this);
        cardHelper.attachToRecyclerView(recyclerView);

        noticeDetailAdapter.setOnitemClickLintener(this);
        HttpResponse.listAnnouncement(token, "", startTime, endTime,1, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                NoticeModel noticeModel = new NoticeModel();
                noticeModel = GsonUtil.GsonToBean(s, NoticeModel.class);
                mylist.addAll(noticeModel.getData());
                noticeDetailAdapter.setDataList(mylist);
                if (mylist.size() == 0) {
                    recyclerView.setVisibility(View.GONE);
                    layout_nodata.setVisibility(View.VISIBLE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    layout_nodata.setVisibility(View.GONE);
                    recyclerView.scrollToPosition(scroll_Toposition);
                }
            }
        });

    }


    @Override
    public void onPageSelected(int position) {

    }

    @OnClick({R.id.text_return, R.id.linear_return, R.id.layout_toolbar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
            case R.id.linear_return:
                finish();
                break;
            case R.id.layout_toolbar:
                break;
        }
    }

    @Override
    public void onItemClick(View view, List<NoticeModel.DataBean> dataList, int position) {
        switch (view.getId()) {
            case R.id.text_left:
                if (position > 0) {
                    int pos = position - 1;
                    recyclerView.scrollToPosition(pos);
                } else if (position == 0) {
                    Toast.makeText(this, "当前已是第一个", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.text_right:
                if (position == dataList.size() - 1) {
                    Toast.makeText(this, "当前已是最后一个", Toast.LENGTH_SHORT).show();
                } else {
                    int pos = position + 1;
                    recyclerView.scrollToPosition(pos);
                }


                break;
        }
    }
}




